package cn.buk.tms.eotms.controller;

import cn.buk.common.JsonResult;
import cn.buk.common.dto.CommonDto;
import cn.buk.tms.eotms.request.ChangeOrderRequestDto;
import cn.buk.tms.eotms.response.ChangeOrderResponseDto;
import cn.buk.tms.eotms.sc.SearchFlightOrderCriteria;
import cn.buk.tms.eotms.service.ChangeOrderService;
import cn.buk.tms.exception.BaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 改签订单
 * @author yfdai
 */
@RestController
@RequestMapping("/flight")
public class ChangeOrderController extends BaseController {

  @Autowired
  private ChangeOrderService changeOrderService;

  /**
   * 查找改签订单
   */
  @GetMapping("/changes")
  public CommonDto<ChangeOrderResponseDto> searchChangeOrders(HttpServletRequest request,
                                                              @ModelAttribute("sc") SearchFlightOrderCriteria sc
  ) {
    List<ChangeOrderResponseDto> datas = changeOrderService
        .searchChangeOrders(getUserId(request), sc);

    CommonDto<ChangeOrderResponseDto> commonDto = new CommonDto<>();
    commonDto.setDataList(datas);
    commonDto.setPage(sc.getPage());

    return commonDto;
  }

  /**
   * 获取改签订单详情
   */
  @GetMapping("/change/{id}")
  public ChangeOrderResponseDto getFlightChangeOrderDetail(HttpServletRequest request,
                                                           @PathVariable("id") int id) throws BaseException {
    return changeOrderService.getFlightChangeOrderDetail(getUserId(request), id);
  }

  /**
   * 创建机票改签订单
   */
  @RequestMapping(value = "/change", consumes = "application/json", method = RequestMethod.POST)
  public JsonResult createFlightChangeOrderV2(HttpServletRequest request,
                                              @RequestBody ChangeOrderRequestDto dto) throws BaseException {

    return changeOrderService.createFlightChangeOrder(getUserId(request), dto);
  }
}
